package cn.itsource.order.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.order.service.IProductOrderService;
import cn.itsource.product.domain.Product;
import cn.itsource.product.query.ProductQuery;
import cn.itsource.product.service.IProductService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IProductOrderService productOrderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;

    //{productId=8, addressId=1, payWay=AliPay}
    /*
        实现的步骤分析：
            1.获取当前的系统登录用户
            2.根据服务【productId】 查询用户购买的Product
            3.根据addressId 查询用户选择的地址
            4.组装OrderProduct 对象  --->根据用户选择的地址【UserAddress】 计算最近的门店
            5.组装OrderProductDetail对象 ---》根据【Product】  和一条订单关联【OrderProduct】
            6.组装OrderAddress对象  -->根据【UserAddress】 和一条订单关联【OrderProduct】

     */

    @PostMapping("/pay")
    public AjaxResult createOrder(@RequestBody Map<String,String> map, HttpServletRequest request){
        try {
            //获取请求头 token
            String token = request.getHeader("U-TOKEN");
            //根据token获取redis里面的数据
            LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
            //根据loginInfo 获取User
            User user = userService.loadByLoginId(loginInfo.getId());
            String result = productOrderService.createOrder(map, user);
            return  AjaxResult.me().setResultObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMessage(e.getMessage());
        }

    }

   
}
